<template>
  <div class="app-container">
    <el-form :inline="true" ref="refForm" :model="form">
      <el-form-item label="查询时间" prop="time">
        <el-date-picker
          v-model="form.time"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item class="with-pre-select" prop="keywords">
        <el-input placeholder="关键词" v-model="form.keywords">
          <el-select slot="prepend" v-model="form.accountType" placeholder="请选择">
            <el-option
              v-for="item in consts.ACCOUNT_TYPE"
              :label="item.title"
              :key="item.value"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-input>
      </el-form-item>

      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="submitFilterForm">搜索</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-download"    @click="exportExcel">导出</el-button>
    </el-form>
    <el-table
      show-summary
      :summary-method="getSummaries"
      border :data="tableData.list" v-loading="loading" element-loading-text="加载中..." class="el-table-align-center">
      <el-table-column label="红利名称" prop="name">
      </el-table-column>
      <el-table-column label="参与人数" prop="count">
      </el-table-column>
      <el-table-column prop="bet" label="金额"></el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="gridSizeChange" @current-change="gridCurrentChange"
                     :current-page="filters.page" :page-sizes="gridPageSize" :page-size="filters.size"
                     :layout="gridLayout" :total="tableData.total">
      </el-pagination>
    </div>
  </div>
</template>


<script>
  import {getStatisticalBetRecharge} from "@/services/api/statistical";
  import {grid ,handleDownload} from '@/build'

  export default {
    name: "statistics-bonus",
    mixins: [grid ,handleDownload],
    data() {
      const {username,time} = this.$route.query;
      const timeArray = time && time.split(',') || []
      const startDate = timeArray[0]?timeArray[0]: moment(consts.ONLINE_DATE).format('YYYY-MM-DD'),
        endDate = timeArray[1]?timeArray[1] : moment().format('YYYY-MM-DD');
      return {
        tableData: {},
        rowData: {},
        form:{
          time:[startDate,endDate],
          accountType: !!username&&!!time?1: -1,
          keywords:username,
        },
        filters:{
          limit: 20,
          page: 1,
          startDate,
          endDate,
          accountType:!!username&&!!time?1: -1,
          keywords:username,
        },
      }
    },
    methods: {
      submitFilterForm(){
        const {time} = this.form;
        let startDate = undefined, endDate= undefined;
        if(!_.isUndefined(time) && _.isArray(time)){
          startDate = time[0];
          endDate= time[1];
        }

        this.filters = {...this.filters,startDate,endDate}
        this.getAll()
      },
      getSummaries(param) {
        const {columns, data} = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '汇总';
            return;
          }
          const values = data.map(item => Number(item[column.property]));
          if ([1].indexOf(index) !== -1 && !values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
          } else {
            sums[index] = '';
          }
        });

        return sums;
      }
    },
    mounted() {
      this.bindGet(getStatisticalBetRecharge, 'tableData', {
        action: 'getGrid',
        sendingData: 'filters'
      });
      this.getAll()
    }

  }
</script>

<style lang="scss" scoped>
  .with-pre-select {
    .el-input-group__prepend {
      .el-select {
        width: 120px;
      }
    }
  }
</style>
